package com.artezio.testapp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.artezio.testapp.domain.Person;
import com.artezio.testapp.service.CityService;
import com.artezio.testapp.service.PersonService;

@Controller
public class PersonController {
	
	private static final Logger logger = LoggerFactory.getLogger(PersonController.class);
	
	@Autowired
	private PersonService personService;
	
	@Autowired
	private CityService cityService;

	/*
	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public String shoHomePage(Model model) {
		
		model.addAttribute("persons", personService.listPerson());
		model.addAttribute("cityList", cityService.listCity());
		model.addAttribute("personFilter", new Person());
		
		return "person";
	}
	*/
	
	@RequestMapping(value = "/person", method = {RequestMethod.POST, RequestMethod.GET})
	public String shoFilteredPage(Model model, Person personFilter) {
			/*
				@RequestParam(value = "personName", required = false) String name,
				@RequestParam(value = "personCity", required = false) Integer cityId,
				@RequestParam(value = "personPhilosopher", required = false) Boolean philosopher,
			*/
		
		model.addAttribute("persons", personService.listPerson(personFilter.getName(), 
				personFilter.getCity(), personFilter.getPhilosopher()));
		
		model.addAttribute("cityList", cityService.listCity());
		model.addAttribute("personFilter", personFilter);
		
		return "person";
	}
	
	@RequestMapping(value = "person/new")
	public String shoNewPerson(Model model){
		logger.info("Request for adding person");
		
		model.addAttribute("person", new Person());
		model.addAttribute("cityList", cityService.listCity());
		
		return "edit-person";
	}
	
	@RequestMapping(value = "person/edit/{personId}")
	public String shoEditPerson(@PathVariable(value = "personId") Integer personId, Model model){
		logger.info("Request for editing person");
		
		model.addAttribute("person", personService.getPerson(personId));
		model.addAttribute("cityList", cityService.listCity());
		
		return "edit-person";
	}
	
	@RequestMapping(value = "person/save")
	public String savePerson(Person person){
		logger.info("Request for saving person");
		
		if(person.getCity().getId()==null){
			person.setCity(null);
		}
		
		personService.addPerson(person);
		
		return "redirect:/person";
	}
	
	@RequestMapping(value = "person/delete/{personId}")
	public String removePerson(@PathVariable(value = "personId") Integer personId){
		logger.info("Request for deliting person");
		
		personService.removePerson(personId);
		
		return "redirect:/person";
	}
	
	
}
